/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dalobject;

import dalInterface.DALRoom;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import utils.DataTable;

/**
 *
 * @author Mrkupi
 */
public class DALRoomImpl implements DALRoom
{
    private Connection conn;
    
    public DALRoomImpl( Connection conn ) throws RemoteException
    {
        this.conn   =   conn;
    }

    @Override
    public boolean insertRoom(int roomID, String roomName) throws RemoteException {
        try {
            Statement st = conn.createStatement();
            String queryString = String.format("insert into ROOM values (\'%s\', \'%s\', \'%s\', \'%s\')", roomID, roomName, 1, 100);
            return st.execute(queryString);
        }
        catch (SQLException ex) {
            Logger.getLogger(DALAccountImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return false;
    }

    @Override
    public boolean deleteRoom(int roomID) throws RemoteException {
                try {
            Statement st = conn.createStatement();
            String queryString = String.format("delete from ROOM where ROOMID=\'%s\'", roomID);
            return st.execute(queryString);
        }
        catch (SQLException ex) {
            Logger.getLogger(DALAccountImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return false;
    }

    @Override
    public DataTable getRoom(int roomID) throws RemoteException 
    {
        try {
            Statement st = conn.createStatement();
            String queryString = String.format("select * from ROOM where ROOMID=\'%s\'", roomID);
            
            return DataTable.createDataTable(st.executeQuery(queryString), "RoomID", "Name", "Password", "Limit");
        } catch (SQLException ex) {
            Logger.getLogger(DALAccountImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return null;
    }    

    @Override
    public int nextRoomID() throws RemoteException
    {
        try {
            Statement st = conn.createStatement();
            String queryString = String.format("SELECT MAX(RoomID) AS MaxID FROM ROOM");
            
            DataTable dtResult = DataTable.createDataTable(st.executeQuery(queryString), "MaxID");
            int currentID = dtResult.get(0).get("MaxID") == null ? 0 : Integer.parseInt(dtResult.get(0).get("MaxID"));
            return currentID + 1;
        } catch (SQLException ex) {
            Logger.getLogger(DALAccountImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return Integer.MAX_VALUE;
    }

    @Override
    public DataTable getAllRoom() throws RemoteException 
    {
        try 
        {
            Statement st = conn.createStatement();
            String queryString = String.format("select * from ROOM");
            
            return DataTable.createDataTable(st.executeQuery(queryString), "RoomID", "Name", "Password", "Limit");
        } catch (SQLException ex)
        {
            Logger.getLogger(DALAccountImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}